package com.A21.Adapter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.A21.Object.A21OrderHeader;
import com.A21MobileSales.OrderSummary;
import com.A21MobileSales.OrderTab;
import com.A21MobileSales.R;
import com.A21MobileSales.SummaryActionBarCallBack;

@SuppressLint({ "ViewHolder", "InflateParams" })
public class OrderSummary_Adapter extends BaseAdapter {
	private ArrayList<A21OrderHeader> lstTransaction;
	private Context context;
	private String Screen = "";
	LinearLayout lnItem;

	public OrderSummary_Adapter(Context _context,
			ArrayList<A21OrderHeader> arr, String _Screen) {
		lstTransaction = arr;
		context = _context;
		Screen = _Screen;
	}

	public int getCount() {
		return lstTransaction.size();
	}

	public Object getItem(int arg0) {
		return lstTransaction.get(arg0);
	}

	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View v, ViewGroup parent) {
		LayoutInflater inflate = ((Activity) context).getLayoutInflater();
		View view = (View) inflate.inflate(R.layout.row_order_summary, null);
		try {
			final A21OrderHeader row = lstTransaction.get(position);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date DateDisp = null;
			try {
				DateDisp = df.parse(row.TrnDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			final DateFormat dfDisp = new SimpleDateFormat("dd-MM-yyyy");
			lnItem = (LinearLayout) view.findViewById(R.id.lnItem);
			TextView txtLine1 = (TextView) view.findViewById(R.id.line1);
			TextView txtLine2 = (TextView) view.findViewById(R.id.line2);
			txtLine1.setText("Trn. Ref. : " + row.DocNo + " Trn Date : "
					+ dfDisp.format(DateDisp));
			txtLine2.setText("Customer : " + row.CName);
			ImageView imgSelect = (ImageView) view
					.findViewById(R.id.Img_Select);
			ImageView imgCheckTrue = (ImageView) view
					.findViewById(R.id.imgCheckTrue);

			imgSelect.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					Bundle b = new Bundle();
					b.putString("Code", row.Code);
					b.putString("CName", row.CName);
					b.putString("Id", String.valueOf(row.Id));
					b.putString("Screen", Screen);
					b.putString("TrnType", row.TrnType);
					Intent _intent = new Intent();
					_intent.putExtras(b);
					_intent.setClass(context, OrderTab.class);
					Activity activity = (Activity) context;
					activity.startActivity(_intent);
				}
			});

			final OrderSummary activity = (OrderSummary) context;
			view.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (row.IsSelected == true) {
						row.IsSelected = false;
						int cntSelected = CountSelected();
						if (cntSelected > 0) {
							activity.mActionMode = activity
									.startActionMode(new SummaryActionBarCallBack(
											activity, cntSelected));
							int doneButtonId = Resources.getSystem()
									.getIdentifier("action_mode_close_button",
											"id", "android");
							View doneButton = activity
									.findViewById(doneButtonId);
							doneButton
									.setOnClickListener(new View.OnClickListener() {
										@Override
										public void onClick(View v) {
											OrderSummary activity = (OrderSummary) context;
											activity.ClearSelection();
											activity.mActionMode.finish();
										}
									});
						} else {
							activity.mActionMode.finish();
						}

						notifyDataSetChanged();
					}

				}
			});

			view.setLongClickable(true);
			view.setOnLongClickListener(new View.OnLongClickListener() {
				@Override
				public boolean onLongClick(View v) {
					row.IsSelected = true;
					int cntSelected = CountSelected();
					if (cntSelected > 0) {
						activity.mActionMode = activity
								.startActionMode(new SummaryActionBarCallBack(
										activity, cntSelected));
						int doneButtonId = Resources.getSystem().getIdentifier(
								"action_mode_close_button", "id", "android");
						View doneButton = activity.findViewById(doneButtonId);
						doneButton
								.setOnClickListener(new View.OnClickListener() {
									@Override
									public void onClick(View v) {
										OrderSummary activity = (OrderSummary) context;
										activity.ClearSelection();
										activity.mActionMode.finish();
									}
								});
					} else {
						activity.mActionMode.finish();
					}

					notifyDataSetChanged();

					return true;
				}
			});
			if (!row.IsSelected) {
				// imgCheckTrue.setVisibility(View.GONE);
				// view.setBackgroundResource(R.drawable.bg_list_item_normal);
				lnItem.setBackgroundResource(R.drawable.bg_ed_login);
			} else {
				// imgCheckTrue.setVisibility(View.VISIBLE);
				lnItem.setBackgroundResource(R.drawable.bg_list_item_selected);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return view;

	}

	private int CountSelected() {
		int selected = 0;
		for (A21OrderHeader row : lstTransaction) {
			if (row.IsSelected) {
				selected++;
			}
		}
		return selected;
	}

}